# -*- coding:utf-8 -*-
# @Author : mistchan
# @Time : 2021/5/29 0029 22:47
# @File : merge_pdfs.py
# @Software: PyCharm
import glob
import os
from PyPDF2 import PdfFileReader, PdfFileMerger
import merge_pictures


def merge_to_one(pdf_file_dirt):
    file_writer = PdfFileMerger()
    for each in pdf_file_dirt:
        # 循环读取需要合并pdf文件
        if each.endswith('.pdf'):
            file_reader = PdfFileReader(each)
            # 遍历每个pdf的每一页

            file_writer.merge(0, file_reader)
    if not os.path.isdir('合并的PDF文件'):
        os.mkdir('合并的PDF文件')
    pdf_merged_file_name = merge_pictures.file_name()
    with open(f'.\\合并的PDF文件\\{pdf_merged_file_name}.pdf', 'wb') as out:
        file_writer.write(out)
    os.startfile(f'.\\合并的PDF文件\\{pdf_merged_file_name}.pdf')


if __name__ == '__main__':
    merge_to_one('1')